package com.hotcode;

/**
 * @Author: huangzhigao
 * @Date: 2023/2/25 19:09
 */
public class leecode200 {
    public int numIslands(char[][] grid) {
        int res = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == '1') {
                    infect(grid, i, j);
                    res++;
                }
            }
        }
        return res;
    }

    public void infect(char[][] grid, int row, int column) {
        if (row < 0 || row >= grid.length || column < 0 || column >= grid[0].length || grid[row][column] != '1') {
            return;
        }
        grid[row][column] = '2';
        infect(grid, row + 1, column);
        infect(grid, row - 1, column);
        infect(grid, row, column + 1);
        infect(grid, row, column - 1);
    }
}
